package org.seedltd.upms.controller.sys;

import org.seedltd.core.base.BaseController;
import org.seedltd.core.utils.Assert;
import org.seedltd.core.utils.BeanUtils;
import org.seedltd.log.annotation.SysLog;
import org.seedltd.upms.model.sys.entity.SysCompany;
import org.seedltd.upms.model.sys.form.SysCompanyForm;
import org.seedltd.upms.model.sys.vo.SysCompanyVO;
import org.seedltd.upms.service.sys.SysCompanyService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import lombok.AllArgsConstructor;

/**
 * 控制器
 *
 * @author laizuan
 * @version 1.0
 * @since 2021/07/19
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sysCompany")
public class SysCompanyController extends BaseController<SysCompanyService> {

    /**
     * 新增
     *
     * @param form
     *            表单数据
     * @return 主键
     */
    @SysLog("新增公司信息")
    @PostMapping(value = "/add")
    @PreAuthorize("hasAuthority('sys:company:add')")
    public Long add(@RequestBody @Validated SysCompanyForm form) {
        return baseService.add(form).getCompanyId();
    }

    /**
     * 获取详情
     *
     * @param id
     *            主键
     * @return
     */
    @GetMapping(value = "/get")
    public SysCompanyVO get(Long id) {
        Assert.notNull(id, "参数不能为空");
        SysCompany pojo = baseService.findById(id);
        SysCompanyVO pojoVo = new SysCompanyVO();
        BeanUtils.copyProperties(pojo, pojoVo);
        return pojoVo;
    }

    /**
     * 修改
     *
     * @param form
     *            表单数据
     * @return 修改状态。true标识修改成功，false标识修改失败
     */
    @SysLog("修改公司信息")
    @PostMapping(value = "/update")
    @PreAuthorize("hasAuthority('sys:company:update')")
    public boolean update(@RequestBody @Validated SysCompanyForm form) {
        return baseService.update(form);
    }

    /**
     * 删除
     *
     * @param id
     *            主键
     * @return 删除状态。true标识修改成功，false标识修改失败
     */
    @SysLog("删除公司信息")
    @GetMapping(value = "/delete")
    @PreAuthorize("hasAuthority('sys:company:delete')")
    public boolean delete(Integer id) {
        Assert.notNull(id, "ID不能为空");
        return baseService.delete(id);
    }

}